home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
CLNEW.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-09-14
|
7KB
|
230 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: clnew.cpp
// Title: C++ Class Libraries
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
// This module contains code to override the global new and delete operators.
//
// The code in this module may be written in C++ or C.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
//
//----------------------------------------------------------------------------
#include <class.hpp>
//----------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------
typedef void (* pvf)();
static pvf _new_handler;
//----------------------------------------------------------------------------
// Un-define the 'new' operator so it can be redefined.
//----------------------------------------------------------------------------
#ifdef new
#undef new
#endif
// will undefine when compiling under MSVC...
#ifndef COMPILER_MSC
//----------------------------------------------------------------------------
// Description: Replacement 'delete' operator.
// Parameters: size Size of buffer to allocate.
// Globals: None modified.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
#if COMPILE_DLL
void _EXPORT_ _LOADDS_ operator delete(void *pv)
#else
void operator delete(void *pv)
#endif
{
if (pv != NULL)
{
MemFree(pv);
}
return ;
}
#endif // ifndef COMPILER_MSC
//----------------------------------------------------------------------------
// Description: Replacement 'new' operator.
// Parameters: size Size of buffer to allocate.
// Globals: None modified.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
void CL_OBJECT::operator delete(void *pv)
{
if (pv)
MemFree(pv);
// ::delete pv; original code
return ;
}
#ifndef COMPILER_MSC
//----------------------------------------------------------------------------
// Description: Replacement 'new' operator.
// Parameters: size Size of buffer to allocate.
// >= 1.
// Globals:
// Returns: Pointer to allocated buffer or NULL.
//----------------------------------------------------------------------------
#if COMPILE_DLL
void * _EXPORT_ _LOADDS_ operator new(size_t size)
#else
void * operator new(size_t size)
#endif
{
if (size <= 0) // Always allocate at least 1 byte
size = 1;
PVOID pv;
do
{
#if COMPILE_DEBUG
pv = MemRealloc_Debug(__FILE__, __LINE__, (SIZET)size, NULL);
#else
pv = MemRealloc((SIZET)size, NULL);
#endif
if (_new_handler != NULL && pv == NULL)
_new_handler();
}
while (pv == NULL && _new_handler != NULL);
return pv;
}
//----------------------------------------------------------------------------
// Description: Replacement 'new' operator.
// Parameters: size Size of buffer to allocate.
// >= 1.
// Globals:
// Returns: Pointer to allocated buffer or NULL.
//----------------------------------------------------------------------------
#if COMPILE_DLL
void * _EXPORT_ _LOADDS_ operator new(size_t size, PCSZ pcszFile, SIZET cLine)
#else
void * operator new(size_t size, PCSZ pcszFile, SIZET cLine)
#endif
{
if (size <= 0) // Always allocate at least 1 byte
size = 1;
PVOID pv;
do
{
pv = MemRealloc_Debug(pcszFile, cLine, (SIZET)size, NULL);
if (_new_handler != NULL && pv == NULL)
_new_handler();
}
while (pv == NULL && _new_handler != NULL);
return pv;
}
#endif // ifndef COMPILER_MSC
//----------------------------------------------------------------------------
// Description: Replacement 'new' operator.
// Parameters: size Size of buffer to allocate.
// >= 1.
// Globals:
// Returns: Pointer to allocated buffer or NULL.
//----------------------------------------------------------------------------
void * _EXPORT_ CL_OBJECT::operator new(size_t size)
{
if (size <= 0) // Always allocate at least 1 byte
size = 1;
PVOID pv;
do
{
#if COMPILE_DEBUG
pv = MemRealloc_Debug(__FILE__, __LINE__, (SIZET)size, NULL);
#else
pv = MemRealloc((SIZET)size, NULL);
#endif
if (_new_handler != NULL && pv == NULL)
_new_handler();
}
while (pv == NULL && _new_handler != NULL);
return pv;
}
//----------------------------------------------------------------------------
// Description: Replacement 'new' operator.
// Parameters: size Size of buffer to allocate.
// >= 1.
// Globals:
// Returns: Pointer to allocated buffer or NULL.
//----------------------------------------------------------------------------
void * _EXPORT_ CL_OBJECT::operator new(size_t size, PCSZ pcszFile, SIZET cLine)
{
if (size <= 0) // Always allocate at least 1 byte
size = 1;
PVOID pv;
do
{
pv = MemRealloc_Debug(pcszFile, cLine, (SIZET)size, NULL);
if (_new_handler != NULL && pv == NULL)
_new_handler();
}
while (pv == NULL && _new_handler != NULL);
return pv;
}
//----------------------------------------------------------------------------
// Description: Memory allocation failure handler.
// Parameters:
// Returns:
//----------------------------------------------------------------------------
#if COMPILE_DLL
pvf _EXPORT_ _LOADDS_ set_new_handler(pvf p)
#else
pvf set_new_handler(pvf p)
#endif
{
pvf t = _new_handler;
_new_handler = p;
return t;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------